﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Calendar {
    public class vCal {

        public List<vEvent> EventList = new List<vEvent>();
        public override string ToString() {
            System.Text.StringBuilder sb = new StringBuilder();

            sb.Append("BEGIN:VCALENDAR\r\n");
            sb.Append("VERSION:1.0\r\n");

            foreach(vEvent evt in EventList) {
                sb.Append(evt.ToString());
            }

            sb.Append("END:VCALENDAR\r\n");
            return sb.ToString();
        }

        public void ParseCal(string Cal) {
            string lineDiv = "\n";
            //if(Cal.IndexOf("\r\n") > 0) lineDiv = "\r\n";
            string[] items = System.Text.RegularExpressions.Regex.Split(Cal, lineDiv);
            bool appending = false;
            System.Text.StringBuilder eventSb = new StringBuilder();
            foreach(string item in items) {

                string iupper = item.ToUpper();
                if(iupper.StartsWith("BEGIN:VEVENT")) appending = true;

                if(appending) {
                    eventSb.Append(item);
                    eventSb.Append(lineDiv);
                }
                if(iupper.StartsWith("END:VEVENT")) {
                    appending = false;
                    vEvent evt = new vEvent();
                    evt.ParseEvent(eventSb.ToString());
                    this.EventList.Add(evt);
                    eventSb = new StringBuilder();
                }
            }
        }
    }
}